import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
#....这样就OK了

# print(tf.__version__)

fashion_mnist = tf.keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

print("image sharp: " + str(train_images[0].shape))
print("label number: " + str(len(train_labels)))
print("images data type: " + str(type(train_images)))
print("label data type: " + str(type(train_labels)))

#plt.figure()
#plt.imshow(train_images[0])
#plt.colorbar()
#plt.grid(False)
#plt.show()

train_images = train_images / 255.0

test_images = test_images / 255.

flattened_images = tf.keras.layers.Flatten(input_shape=(28, 28))

model = tf.keras.Sequential([
    flattened_images,
    tf.keras.layers.Dense(256, activation=tf.nn.softplus),
    tf.keras.layers.Dense(128, activation=tf.nn.relu),
    tf.keras.layers.Dense(10)
])


model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

model.fit(train_images, train_labels, epochs=10)

test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)

print('\nTest accuracy:', test_acc)

probability_model = tf.keras.Sequential([model, 
                                         tf.keras.layers.Softmax()])

predictions = probability_model.predict(test_images)
index = 10 
print("predictions[" + str(index) + "]" + str(predictions[index]))
print("highest confidence value:" + str(np.argmax(predictions[index])) + " and label:" + str(test_labels[index]) + " and class:" + class_names[np.argmax(predictions[index])])